home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8210 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  42 lines

  1. Path: sun001.spd.dsccc.com!spd!jmccarty
  2. From: jmccarty@spd.dsccc.com (Mike McCarty)
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: 15 Feb 1996 19:20:30 GMT
  6. Organization: DSC Communications Corporation, Plano, Texas USA
  7. Message-ID: <4g011u$gna@sun001.spd.dsccc.com>
  8. References: <4fr8be$ass@news.iconn.net> <31224679.6193@born.com>
  9. NNTP-Posting-Host: aplo139.spd.dsccc.com
  10.  
  11. In article <31224679.6193@born.com>, John Cleland  <clelaj@born.com> wrote:
  12. )The Crow wrote:
  13. )> 
  14. )> Here is what I am trying to do, I want to find the last NON-ZERO digit of a
  15. )> given factorial.  For instance,
  16. )> 
  17. )> 5! = 120
  18. )> 
  19. )> so the last non-zero digit is 2.  I want to be able to do this up to 1000.
  20. )> Problem is, no matter how huge of a data-type you use, there isn't any way for
  21. )> the computer to handle 1000!, it is however possible to find the last non-zero
  22. )> digit somehow.  One thing I have tried is as I went through mulitplying the
  23. )> series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the
  24. )> trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i
  25. )> am not really sure why.  If anyone has a clue how I can do this let me know.
  26. )
  27. )Don't just strip the trailing zeros, remove all digits except the last non-zero
  28. )digit (which is your output) and then multiply by the next number in your sequence.
  29. )This keeps the numbers down to a managable level and gives the correct answer as
  30. )no more significant digit can affect the value of the LSD.
  31.  
  32. Per my previous reply: Perhaps I didn't go far enough. Keeping the 2 in
  33. 5! etc. works up to a point, after which the problem begins to show up
  34. later in the game. Eventually you need to keep three, then four digits,
  35. etc. 
  36.  
  37. Mike
  38. ----
  39. char *p="char *p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
  40.  
  41. I don't speak for DSC.         <- They make me say that.
  42.